package org.example.springcloud.controller;

import org.example.springcloud.service.IFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;
import java.util.List;

@RestController
public class TestController {

//    private static final String SERVICE_PROVIDER_ADDRESS = "http://nacosmodule";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private IFeignService iFeignService;

    @GetMapping("/restTemplate/list")
    public String getRestTemplateList() {
        List<ServiceInstance> instances = discoveryClient.getInstances("nacosmodule");
//        Iterator<ServiceInstance> iterator = instances.iterator();
//        while (iterator.hasNext())
//        {
//            ServiceInstance next = iterator.next();
//            System.out.println(next);
//        }
//        System.out.println("-----------------");
        ServiceInstance serviceInstance = instances.get(0);

       return restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/user/list", String.class);
    }

    @GetMapping("/feign/list")
    public String getFeignList() {
       return iFeignService.getList();
    }

}
